package com.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.admin.domain.*;
import com.admin.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.admin.service.TransactionService;
import org.springframework.util.StringUtils;

@Slf4j
@Service
public class TransactionServiceImpl implements TransactionService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private SysParamMapper sysParamMapper;



    @Transactional
    @Override
    public void saveAdminDO(AdminDO adminDO, List<Long> addRoleIds) {
        adminMapper.insertSelective(adminDO);
        List<AdminRoleDO> adminRoleDOs = new ArrayList<>();
        for (Long roleId : addRoleIds) {
            AdminRoleDO adminRoleDO = new AdminRoleDO();
            adminRoleDO.setUserId(adminDO.getUserId());
            adminRoleDO.setRoleId(roleId);
            adminRoleDOs.add(adminRoleDO);
        }
        adminRoleMapper.insertBatch(adminRoleDOs);
    }

    @Transactional
    @Override
    public void updateAdminDO(AdminDO adminDO, List<Long> delRoleIds, List<Long> addRoleIds) {
        //如果是没有选择分区- 则是整个区域
        /*if (StringUtils.isEmpty(adminDO.getRegionCitys()) || "0".equals(adminDO.getRegionCitys())) {
            SysParam sysParam = sysParamMapper.selectByPrimaryKey(adminDO.getRegionId());
            if (!Objects.isNull(sysParam) && !StringUtils.isEmpty(sysParam.getValue())) {
                adminDO.setRegionCitys(sysParam.getValue());
            }
        }*/
        adminMapper.updateByPrimaryKeySelective(adminDO);
        if (CollectionUtils.isNotEmpty(delRoleIds)) {
            adminRoleMapper.deleteByUserRoleId(adminDO.getUserId(), delRoleIds);
        }
        if (CollectionUtils.isNotEmpty(addRoleIds)) {
            List<AdminRoleDO> adminRoleDOs = new ArrayList<AdminRoleDO>();
            for (Long roleId : addRoleIds) {
                AdminRoleDO adminRoleDO = new AdminRoleDO();
                adminRoleDO.setUserId(adminDO.getUserId());
                adminRoleDO.setRoleId(roleId);
                adminRoleDOs.add(adminRoleDO);
            }
            adminRoleMapper.insertBatch(adminRoleDOs);
        }
    }

    @Transactional
    @Override
    public void deleteAdmin(Integer userId) {
        adminMapper.deleteByPrimaryKey(userId);
        adminRoleMapper.deleteByUserId(userId);
    }

    @Transactional
    @Override
    public void deleteAdmins(List<Integer> userIds) {
        adminMapper.deleteByPrimaryKeys(userIds);
        adminRoleMapper.deleteByUserIds(userIds);
    }

    @Transactional
    @Override
    public void saveRoleDO(RoleDO roleDO, List<Long> addMenuIds) {
        if (!StringUtils.isEmpty(roleDO.getResponsibleAreaId())) {
            String[] ids = roleDO.getResponsibleAreaId().split("-");
            if (null != ids && ids.length > 0) {
                roleDO.setBusinessAreaId(Integer.parseInt(ids[ids.length-1]));
            }
         }
        roleMapper.insertSelective(roleDO);
        List<RoleMenuDO> roleMenuDOs = new ArrayList<RoleMenuDO>();
        for (Long menuId : addMenuIds) {
            RoleMenuDO roleMenuDO = new RoleMenuDO();
            roleMenuDO.setRoleId(roleDO.getRoleId());
            roleMenuDO.setMenuId(menuId);
            roleMenuDOs.add(roleMenuDO);
        }
        roleMenuMapper.insertBatch(roleMenuDOs);
    }

    @Transactional
    @Override
    public void updateRoleDO(RoleDO roleDO, List<Long> delMenuIds, List<Long> addMenuIds) {
        if (!StringUtils.isEmpty(roleDO.getResponsibleAreaId())) {
            String[] ids = roleDO.getResponsibleAreaId().split(",");
            if (null != ids && ids.length > 0) {
                roleDO.setBusinessAreaId(Integer.parseInt(ids[ids.length-1]));
            }
        }
        roleMapper.updateByPrimaryKeySelective(roleDO);
        if (CollectionUtils.isNotEmpty(delMenuIds)) {
            roleMenuMapper.deleteByRoleMenuId(roleDO.getRoleId(), delMenuIds);
        }
        if (CollectionUtils.isNotEmpty(addMenuIds)) {
            List<RoleMenuDO> roleMenuDOs = new ArrayList<>();
            for (Long menuId : addMenuIds) {
                RoleMenuDO roleMenuDO = new RoleMenuDO();
                roleMenuDO.setRoleId(roleDO.getRoleId());
                roleMenuDO.setMenuId(menuId);
                roleMenuDOs.add(roleMenuDO);
            }
            roleMenuMapper.insertBatch(roleMenuDOs);
        }
    }

    @Transactional
    @Override
    public void updateRoleNew(RoleDO roleDO, List<Long> menuIds) {
        //修改
        roleMapper.updateByPrimaryKeySelective(roleDO);
        //删除
        roleMenuMapper.deleteByRoleId(roleDO.getRoleId());
        //新增
        if (CollectionUtils.isNotEmpty(menuIds)) {
            List<RoleMenuDO> roleMenuDOs = new ArrayList<>();
            for (Long menuId : menuIds) {
                RoleMenuDO roleMenuDO = new RoleMenuDO();
                roleMenuDO.setRoleId(roleDO.getRoleId());
                roleMenuDO.setMenuId(menuId);
                roleMenuDOs.add(roleMenuDO);
            }
            roleMenuMapper.insertBatch(roleMenuDOs);
        }
    }

    @Transactional
    @Override
    public void deleteRole(Long roleId) {
        roleMapper.deleteByPrimaryKey(roleId);
        adminRoleMapper.deleteByRoleId(roleId);
        roleMenuMapper.deleteByRoleId(roleId);
    }

    @Transactional
    @Override
    public void deleteRoles(List<Long> roleIds) {
        roleMapper.deleteByPrimaryKeys(roleIds);
        adminRoleMapper.deleteByRoleIds(roleIds);
        roleMenuMapper.deleteByRoleIds(roleIds);
    }

    @Transactional
    @Override
    public void deleteMenus(List<Long> menuIds) {
        menuMapper.deleteByPrimaryKeys(menuIds);
        roleMenuMapper.deleteByMenuIds(menuIds);
    }




}
